﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZEFIT
{
    public partial class NewRapportForm : Form
    {
        Controller controller = null;
        Rapport dataObject = null;
        RapportsForm cForm = null;

        public NewRapportForm(RapportsForm cForm)
        {
            this.cForm = cForm;
            InitializeComponent();
            controller = new Controller(new MYSQLAdabterRapport());
            this.dataObject = new Rapport();
            loadCategoryList();
            loadProjectList();
            cbx_category.SelectedIndex = 0;
            cbx_project.SelectedIndex = 0;
            tbx_user.Text = cForm._mainForm.user.UserName;
        }

        public NewRapportForm(RapportsForm cForm, Rapport dataObject)
        {
            this.cForm = cForm;
            InitializeComponent();
            controller = new Controller(new MYSQLAdabterRapport());
            this.dataObject = dataObject;
            loadCategoryList();
            loadProjectList();
            int i = 0;
            foreach (WorkingCategory temp in cbx_category.Items)
            {
                if (dataObject.Category.Id == temp.Id)
                {
                    cbx_category.SelectedIndex = i;
                }
                i++;
            }
            int e = 0;
            foreach (Project temp in cbx_project.Items)
            {
                if (dataObject.Project.Id == temp.Id)
                {
                    cbx_project.SelectedIndex = e;
                }
                e++;
            }
            date_date.Value = dataObject.WorkedFrom;
            tbx_from.Text = dataObject.WorkedFrom.ToString("HH:mm");
            tbx_to.Text = dataObject.WorkedTo.ToString("HH:mm");
            tbx_user.Text = cForm._mainForm.user.UserName;
        }

        private void loadCategoryList()
        {
            foreach (List<object> tempList in new Controller(new MYSQLAdapterWorkingCategory()).Select())
            {

                WorkingCategory temp = new WorkingCategory();
                temp.Id = (int)tempList[0];
                temp.Name = (String)tempList[1];
                cbx_category.Items.Add(temp);
            }

            cbx_category.DisplayMember = "Name";
            cbx_category.ValueMember = "Id";
        }

        private void loadProjectList()
        {
            foreach (List<object> tempList in new Controller(new MYSQLAdabterProject()).Select())
            {

                Project temp = new Project();
                temp.Id = (int)tempList[0];
                temp.Name = (String)tempList[1];
                cbx_project.Items.Add(temp);
            }

            cbx_project.DisplayMember = "Name";
            cbx_project.ValueMember = "Id";
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            lbl_from_error.Visible = false;
            lbl_to_error.Visible = false;

            dataObject.User = cForm._mainForm.user;
            WorkingCategory tempCategory = (WorkingCategory)cbx_category.SelectedItem;
            dataObject.Category = tempCategory;
            Project tempProject = (Project)cbx_project.SelectedItem;
            dataObject.Project = tempProject;

            try
            {
                String date = date_date.Value.Day + "." + date_date.Value.Month + "." + date_date.Value.Year;
                String fromTime = tbx_from.Text;
                String toTime = tbx_to.Text;
                dataObject.WorkedFrom = Convert.ToDateTime(date + " " + fromTime);
                dataObject.WorkedTo = Convert.ToDateTime(date + " " + toTime);
            }
            catch
            {
                lbl_from_error.Visible = true;
                lbl_to_error.Visible = true;
                return;
            }

            controller.Save(dataObject);
            cForm.dataLoad();
            this.Close();
        }

        private void but_back_Click(object sender, EventArgs e)
        {
            cForm.dataLoad();
            this.Close();
        }
    }
}
